﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Comm.Model.Enums;
using Comm.Model;

namespace Comm.BLL
{
    public partial class UserServices
    {
        Comm.DAL.UsersDAO _dao = new Comm.DAL.UsersDAO();
        BLL.UserTypeInfoService bllusertype = new UserTypeInfoService();
        public RegisterResult Register(string username, string password, out Users user)
        {
            var exit = _dao.QueryCount(new { uLoginName = username }) > 0;
            if (exit)
            {
                user = null;
                return RegisterResult.用户名已存在;
            }
            user = new Users();
            user.uLoginName = username;
            user.uPwd = password.Md5().ToUpper();
            //user.otherName = null;
            user.uId = _dao.Insert(user);
            if (user.uId == 0)
            {
                return RegisterResult.注册失败;
            }
            UserTypeInfo model =new UserTypeInfo ()
            {
                 authorityId=3,
                  userId=user.uId
            };
            bllusertype.Insert(model);
            return RegisterResult.注册成功;
        }
        public LoginResult Login(string username, string password, out Users user)
        {
            user = null;
            // 根据用户名检索数据
            var temp = _dao.QuerySingle(new { uLoginName = username });

            // 判断有没有查到
            if (temp.uLoginName == null)
            {
                return LoginResult.用户名不存在;
            }

            // 用户存在
            if (temp.uPwd != password.Md5()) // 密码加密
            {
                return LoginResult.密码错误;
            }
            user = temp;
            return LoginResult.登录成功;
        }

        public LoginResult CookieLogin(string username, string password, out Users user)
        {
            user = null;
            // 根据用户名检索数据
            var temp = _dao.QuerySingle(new { uLoginName = username });

            // 判断有没有查到
            if (temp == null)
            {
                return LoginResult.用户名不存在;
            }

            // 用户存在
            if (temp.uPwd != password.Md5().ToUpper() && temp.uPwd.Trim() != password) // 密码无需加密比对  因为cookie中本来就是加密的
            {
                return LoginResult.密码错误;
            }

            user = temp;
            return LoginResult.登录成功;
        }
    }
}
